package com.qding.park.adapter.test.adapter.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.system.NetWorkException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.pojo.ap.ApMonthFeeOrder;
import com.qding.park.manager.pojo.ap.ApTempPayOrder;
import com.qding.park.target.common.AdapterParam;
import com.qding.park.target.common.adapter.PayFeeAdapter;
import com.qding.park.target.common.entity.AdMonthFeeOrder;
import com.qding.park.target.common.entity.AdTempPayOrder;
import com.qding.park.target.common.entity.AdTempPayResult;

/**
 * @ClassName PayFeeAdapterImpl
 * @Description TODO(缴费适配器实现)
 * @author Administrator
 * @Date 2016年8月28日 下午9:47:06
 * @version 1.0.0
 */
public class PayFeeAdapterImpl extends AdapterParam implements PayFeeAdapter {
	private static final Log _log = LogFactory.getLog(PayFeeAdapterImpl.class);
	
	
	public PayFeeAdapterImpl(String url) {
		this.setAdapterUrl(url);
	}

	/*
	 * (非 Javadoc) Description:
	 * 
	 * @see
	 * com.qding.park.target.common.adapter.PayFeeAdapter#createTempPayment(java
	 * .lang.String, java.lang.String) 创建 临停缴费单
	 */
	@Override
	public AdTempPayOrder createTempPayment(String parkId, String carNum,String userId) throws NetWorkException, AdapterException {
		try {
//			String url = this.getAdapterUrl();
//			JSONObject jsonObj = new JSONObject();
//			jsonObj.put("clientID", parkId);
//			jsonObj.put("cardCode", 10);
//			jsonObj.put("plateNo", carNum);
//			jsonObj.put("flag", "1");
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("UserID", "110");
//			params.put("RequestFun", ConstantFunction.Parking_GetParkingMoney);
//			params.put("RequestData", jsonObj.toJSONString());
//			params.put("Sign", "ddd");
//			_log.info("adapter request:"+params);
//			String result = HttpUtil.doPost(this.getAdapterUrl(), params);
//			_log.info("adapter response:"+result);
//			java.io.Reader in = new StringReader(result);
//			Document doc = (new SAXBuilder()).build(in);
//
//			String temp = doc.getRootElement().getValue();
//			in.close();
//			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
//			String errorCode = repmsg.getErrorCode();
//			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
//				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
//						ExceptionMessage.createExceptionMessage(
//								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
//								ConstantFunction.Parking_GetParkingMoney + " adpater处理出错 :"+repmsg.getErrorMsg()));
//			}
//			JSONObject data = (JSONObject) repmsg.getResult();
//
//			String enterTime = data.getString("enterTime");
//			String parkingOrder = data.getString("parkingOrder");
//			String parkingSerial = data.getString("parkingSerial");
//			int parkingTime = Integer.valueOf(data.getString("parkingTime"));
//			int payValue = Integer.valueOf(data.getString("payValue"));
		    AdTempPayOrder apTempPayOrder = new AdTempPayOrder();
			apTempPayOrder.setEnterTime(DateUtils.parseDate("201609181500", DateUtils.DATE_LONG_DATE));
			apTempPayOrder.setOrderId("qqqqqqqqqqq");
			apTempPayOrder.setParkingSerial("ssssssssss");
			apTempPayOrder.setParkingTime(1200);
			apTempPayOrder.setPayValue(1200);
			return apTempPayOrder;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public AdMonthFeeOrder calMonthFee(String parkId, int monthNum, String carNum)
			throws NetWorkException, AdapterException {
		try {
//			String url = this.getAdapterUrl();
//			JSONObject jsonObj = new JSONObject();
//			jsonObj.put("clientID", parkId);
//			jsonObj.put("cardCode", "19");
//			jsonObj.put("plateNo", carNum);
//			jsonObj.put("flag", "1");
//			jsonObj.put("payMos", String.valueOf(monthNum));
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("UserID", "110");
//			params.put("RequestFun", ConstantFunction.Parking_MonthPayPrep);
//			params.put("RequestData", jsonObj.toJSONString());
//			params.put("Sign", "ddd");
//			_log.info("adapter request:"+params);
//			String result = HttpUtil.doPost(this.getAdapterUrl(), params);
//			_log.info("adapter response:"+result);
//			java.io.Reader in = new StringReader(result);
//			Document doc = (new SAXBuilder()).build(in);
//
//			String temp = doc.getRootElement().getValue();
//			in.close();
//			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
//			String errorCode = repmsg.getErrorCode();
//			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
//				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
//						ExceptionMessage.createExceptionMessage(
//								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
//								ConstantFunction.Parking_MonthPayPrep + " adpater处理出错 ："+repmsg.getErrorMsg()));
//			}
			// {"startDate":"20161001","totalFee":"20000","expireDate":"20161031"}
//			JSONObject data = (JSONObject) repmsg.getResult();
		    AdMonthFeeOrder apMonthFeeOrder = new AdMonthFeeOrder();
			apMonthFeeOrder.setParkId(parkId);
			apMonthFeeOrder.setMonthNum(monthNum);
			apMonthFeeOrder.setTotalFee(monthNum*10000);
			apMonthFeeOrder.setStartTime(DateUtils.parseDate("20160918", DateUtils.DATE_SHORT_DATEONLY));
			apMonthFeeOrder
					.setExpireDate(DateUtils.parseDate("20161018", DateUtils.DATE_SHORT_DATEONLY));
			return apMonthFeeOrder;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public void tempPaySuccessNotice(AdTempPayResult tempPayResult)
			throws NetWorkException, AdapterException {
		try {
//			String url = this.getAdapterUrl();
//			JSONObject jsonObj = new JSONObject();
//			jsonObj.put("clientID", apTempPayOrder.getParkId());
//			jsonObj.put("parkingOrder", apTempPayOrder.getOrderId());
//			jsonObj.put("payTime", DateUtils.formatDate(apTempPayOrder.getPayTime(), DateUtils.DATE_LONG_DATE));
//			jsonObj.put("payValue", apTempPayOrder.getPayValue());
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("UserID", "110");
//			params.put("RequestFun", ConstantFunction.Parking_GetParkingMoneyResult);
//			params.put("RequestData", jsonObj.toJSONString());
//			params.put("Sign", "ddd");
//			_log.info("adapter request:"+params);
//			String result = HttpUtil.doPost(this.getAdapterUrl(), params);
//			_log.info("adapter response:"+result);
//			java.io.Reader in = new StringReader(result);
//			Document doc = (new SAXBuilder()).build(in);
//
//			String temp = doc.getRootElement().getValue();
//			in.close();
//			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
//			String errorCode = repmsg.getErrorCode();
//			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
//				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
//						ExceptionMessage.createExceptionMessage(
//								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
//								ConstantFunction.Parking_GetParkingMoneyResult + " adpater处理出错 :"+repmsg.getErrorMsg()));
//			}
//			// {"startDate":"20161001","totalFee":"20000","expireDate":"20161031"}
//			return new AdTempPayOrder();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public AdMonthFeeOrder monthPay(String parkId, String carNum, int monthNum, int amount, Date payDate,
			String chargePaidNo) throws NetWorkException, AdapterException {
		try {
//			String url = this.getAdapterUrl();
//			JSONObject jsonObj = new JSONObject();
//			jsonObj.put("clientID", parkId);
//			jsonObj.put("cardCode", 19);
//			jsonObj.put("plateNo", carNum);
//			jsonObj.put("flag", "1");
//			jsonObj.put("payMos", String.valueOf(monthNum));
//			jsonObj.put("amount", String.valueOf(amount));
//			jsonObj.put("payDate", DateUtils.formatDate(payDate, DateUtils.DATE_LONG_DATE));
//			jsonObj.put("chargePaidNo", chargePaidNo);
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("UserID", "110");
//			params.put("RequestFun", ConstantFunction.Parking_MonthlyFee);
//			params.put("RequestData", jsonObj.toJSONString());
//			params.put("Sign", "ddd");
//			_log.info("adapter request:"+params);
//			String result = HttpUtil.doPost(this.getAdapterUrl(), params);
//			_log.info("adapter response:"+result);
//			java.io.Reader in = new StringReader(result);
//			Document doc = (new SAXBuilder()).build(in);
//
//			String temp = doc.getRootElement().getValue();
//			in.close();
//			ResponseMsg repmsg = JSONObject.parseObject(temp, ResponseMsg.class);
//			String errorCode = repmsg.getErrorCode();
//			if (!ErrorCodeEnum.SUCCESS.getCode().equals(errorCode)) {
//				throw new AdapterException(AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getId(), null,
//						ExceptionMessage.createExceptionMessage(
//								AdapterException.TypeEnum.EXCEPTION_ADAPTER_FAIL.getDesc(), repmsg.getErrorMsg(),
//								ConstantFunction.Parking_MonthlyFee + " adpater处理出错 :"+repmsg.getErrorMsg()));
//			}
			// {"startDate":"20161001","totalFee":"20000","expireDate":"20161031"}
//			JSONObject data = (JSONObject) repmsg.getResult();
		    AdMonthFeeOrder apMonthFeeOrder = new AdMonthFeeOrder();
			apMonthFeeOrder.setCarNum(carNum);
			apMonthFeeOrder
					.setExpireDate(DateUtils.parseDate("20161009", DateUtils.DATE_SHORT_DATEONLY));
			return apMonthFeeOrder;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

}
